package com.all.play.mymvptwo;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.mymvptwo.adapter.MyAdapter;
import com.all.play.mymvptwo.bean.InfoBean;
import com.all.play.mymvptwo.presenter.DataPersenter;
import com.all.play.mymvptwo.view.BaseActivity;
import com.all.play.mymvptwo.view.IView;

import java.util.List;

public class MainActivity extends BaseActivity<IView,DataPersenter<IView>> implements IView{

    private ListView listView;
    private MyAdapter myAdapter;
    private ProgressBar progressBar;
    private TextView tv_pro;
    private TextView tv_total;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();

    }



    private void initView() {
        listView = (ListView) findViewById(R.id.lv);
        progressBar = (ProgressBar) findViewById(R.id.pb);
        tv_pro = (TextView) findViewById(R.id.tv_pro);
        tv_total = (TextView) findViewById(R.id.tv_total);
        //dataPersenter=new DataPersenter<MainActivity>();

        //dataPersenter.attachView(this);
        //P层对象通过调用fetch方法,是view层获得数据,同过下方重写的方法,将数据会传到当前Activity上
        dataPersenter.fetch();
    }


    @Override
    public DataPersenter<IView> creatPresenter() {
        DataPersenter<IView> iViewDataPersenter = new DataPersenter<>();

        return iViewDataPersenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //dataPersenter.detachView();
    }

    @Override
    public void showData(List<InfoBean.DataBean> datas) {
        myAdapter = new MyAdapter(MainActivity.this, datas);
        listView.setAdapter(myAdapter);

    }

    @Override
    public void showProgress() {
        Toast.makeText(MainActivity.this, "进度条加载", Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.VISIBLE);

    }

    @Override
    public void hidePeogress() {
        progressBar.setVisibility(View.GONE);
        Toast.makeText(MainActivity.this, "进度条隐藏", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDetialProgress(long total, float progress) {

        tv_pro.setText(progress+"");
        tv_total.setText(total+"");
    }


}
